// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
// template <typename T>

#pragma once

#include <tuple>
#include <nebula/util/string.h>

using nebula::internal::ToChars;

namespace nebula {
namespace internal {

namespace detail {

template <typename OStream, typename Tuple, size_t N>
struct TuplePrinter {
  static void Print(OStream* os, const Tuple& t) {
    TuplePrinter<OStream, Tuple, N - 1>::Print(os, t);
    *os << std::get<N - 1>(t);
  }
};

template <typename OStream, typename Tuple>
struct TuplePrinter<OStream, Tuple, 0> {
  static void Print(OStream* os, const Tuple& t) {}
};

}  // namespace detail

// Print elements from a tuple to a stream, in order.
// Typical use is to pack a bunch of existing values with std::forward_as_tuple()
// before passing it to this function.
template <typename OStream, typename... Args>
void PrintTuple(OStream* os, const std::tuple<Args&...>& tup) {
  detail::TuplePrinter<OStream, std::tuple<Args&...>, sizeof...(Args)>::Print(os, tup);
}

template <typename Range, typename Separator>
struct PrintVector {
  const Range& range_;
  const Separator& separator_;

  template <typename Os>  // template to dodge inclusion of <ostream>
  friend Os& operator<<(Os& os, PrintVector l) {
    bool first = true;
    os << "[";
    for (const auto& element : l.range_) {
      if (first) {
        first = false;
      } else {
        os << l.separator_;
      }
      os << ToChars(element);  // use ToChars to avoid locale dependence
    }
    os << "]";
    return os;
  }
};
template <typename Range, typename Separator>
PrintVector(const Range&, const Separator&) -> PrintVector<Range, Separator>;
}  // namespace internal
}  // namespace nebula
